package com.itheima.blog.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.client.AdminClient;
import com.itheima.api.client.dto.CategoryDto;
import com.itheima.api.client.dto.TagDto;
import com.itheima.blog.web.domain.po.TBlog;
import com.itheima.blog.web.domain.po.TComment;
import com.itheima.blog.web.domain.query.BlogPageQuery;
import com.itheima.blog.web.mapper.TBlogMapper;
import com.itheima.blog.web.service.ITBlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.blog.web.service.ITCommentService;
import com.itheima.common.domain.PageResDto;
import com.itheima.common.utils.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 博客文章表 服务实现类
 * </p>
 *
 * @author sxm
 * @since 2023-07-11
 */
@Service
@RequiredArgsConstructor

public class TBlogServiceImpl extends ServiceImpl<TBlogMapper, TBlog> implements ITBlogService {
    private final AdminClient adminClient;
    private final ITCommentService commentService;

    //分页查询
    @Override
    public PageResDto<TBlog> getBlogPages(BlogPageQuery blogPageQuery) {
        //获取博客标题
        String title = blogPageQuery.getTitle();
        //获得分类名称
        String categoryName = blogPageQuery.getCategoryName();
        Integer categoryId = null;
        if (categoryName != null) {
            CategoryDto categoryDto = adminClient.getCategoryByName(categoryName);
            categoryId = categoryDto.getId();
        }
        //通过feign接口查询分类
        /*CategoryDto categoryDto = adminClient.getCategoryByName(categoryName);
        categoryId = categoryDto.getId();*/
        //获取标签名称
        String tagName = blogPageQuery.getTagName();
        Integer tagId = null;
        if (tagName != null) {
            TagDto tagDto = adminClient.getTagByName(tagName);
            tagId = tagDto.getId();
        }
        //通过feign接口查询标签
        /*TagDto tagDto = adminClient.getTagByName(tagName);
        Integer tagId = tagDto.getId();*/
        //根据标签名称获取标签id
        Page<TBlog> page = lambdaQuery().like(TBlog::getTitle, title)
                .eq(categoryId != null, TBlog::getCategoryId, categoryId)
                .eq(tagId != null, TBlog::getTagId, tagId)
                .page(blogPageQuery.toMpPageDefaultSortByCreateTimeDesc());
        List<TBlog> blogList = page.getRecords();
        //总数
        Integer total = (int) page.getTotal();
        //当前页
        Integer current = (int) page.getCurrent();
        //每页显示条数
        Integer pages = (int) page.getPages();
        PageResDto<TBlog> blogPageResDto = new PageResDto<TBlog>();
        blogPageResDto.setTotal(total);
        blogPageResDto.setCurrentPage(current);
        blogPageResDto.setSize(pages);
        blogPageResDto.setData(blogList);
        return blogPageResDto;
    }

    @Override
    public boolean removeBlodById(Long blogId) {
        LambdaQueryWrapper<TComment> commentWrapper = new LambdaQueryWrapper<>();
        commentWrapper.eq(TComment::getBlogId, blogId);
        commentService.remove(commentWrapper);
        return removeById(blogId);
    }

    @Override
    public boolean changeBlogComment(TBlog comment) {
        return updateById(comment);
    }

    @Override
    public boolean chengePublish(TBlog tBlog) {
        return updateById(tBlog);
    }
}
